package com.chain.BrotherExchange.ui.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.Quotation;

import java.util.List;

/**
 * 行情
 */
public class QuotationListAdapter extends RecyclerView.Adapter<QuotationListAdapter.ViewHolder> {

    private List<Quotation.DataBean.ListBean> mQuotationList;
    private AdapterView.OnItemClickListener mOnItemClickListener;

    public QuotationListAdapter() {

    }

    public QuotationListAdapter(List<Quotation.DataBean.ListBean> quotationList) {
        this.mQuotationList = quotationList;
    }

    @NonNull
    @Override
    public QuotationListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_quotation_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull QuotationListAdapter.ViewHolder holder, int i) {
        Quotation.DataBean.ListBean quotation = mQuotationList.get(i);
        holder.mTypeText.setText(quotation.getSymbol());
        holder.mVolatilityText.setText(quotation.getScope());
        holder.mPriceText.setText(quotation.getPrice());
        holder.mTurnoverText.setText("24h " + quotation.getAmount());
        holder.mCnyPriceText.setText(quotation.getCny_price());
        holder.mVolatilityText.setBackgroundResource(quotation.getScopeBackground());

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(v ->
                    mOnItemClickListener.onItemClick(null, v, i, 0));
        }
    }

    public Quotation.DataBean.ListBean getItem(int position){
        return mQuotationList.get(position);
    }

    public void setQuotationList(List<Quotation.DataBean.ListBean> quotationList) {
        this.mQuotationList = quotationList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ListView.OnItemClickListener itemClickListener) {
        this.mOnItemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return mQuotationList != null ? mQuotationList.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTypeText;
        private final TextView mPriceText;
        private final TextView mTurnoverText;
        private final TextView mCnyPriceText;
        private final TextView mVolatilityText;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTypeText = itemView.findViewById(R.id.type_text);
            mTurnoverText = itemView.findViewById(R.id.turnover_text);
            mPriceText = itemView.findViewById(R.id.price_text);
            mCnyPriceText = itemView.findViewById(R.id.cny_price_text);
            mVolatilityText = itemView.findViewById(R.id.volatility_text);
        }
    }
}
